package controller;


import entity.SysUser;
import org.apache.log4j.Logger;
import org.springframework.cglib.core.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * SysUserController
 *
 * @author boy
 * @since 2023/10/25
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    private static final String USER_SESSION = null;
    private Logger logger=Logger.getLogger(SysUserController.class);
private ArrayList<SysUser> sysUsers=new ArrayList<>();
private ArrayList<SysUser>querylist=new ArrayList<>();
@Resource
private SysUserService sysUserService;

    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping(value = "/toLogin")
public String login(){
    logger.info("跳转到登录页面");
    return "login";
}

    /**
     * 登录
     * @param account
     * @param password
     * @return
     */
    @PostMapping("/login")
public String doLogin(HttpServletRequest request, HttpSession session,@RequestParam String account, @RequestParam String password){
    logger.debug("登录方法");
    SysUser sysUser=sysUserService.login(account,password);
    if (null!=sysUser){
        session.setAttribute(USER_SESSION, sysUser);
        return "redirect:/user/toMain";
    }else{
        request.setAttribute("error","用户名密码不正确");
        return "login";
    }
}

    /**
     * 跳转到首页
     * @return
     */
    @RequestMapping("/toMain")
public String main(HttpSession session){
        if (session.getAttribute(USER_SESSION)==null){
            return "redirect:/user/toLogin";
        }
    return "frame";
}
public SysUserController(){
    try{
        sysUsers.add(new SysUser(5,"zhaojing","赵静","555555",1,"13054784445","上海宝山区",
                                 1,1,new Date(),1,new Date()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    @GetMapping("/list")
public String list(Model model,String queryRealName,Integer queryRoleId,@RequestParam(defaultValue = "1")Integer pageIndex){
        List<SysUser>userList=null;
        try{
            int pageSize=Constants.pageSize;
            int totalCount=sysUserService.getUserCount(queryRealName,queryRoleId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("当条件为空时，查询用户信息");
        return "redirect:/user/toSysError";
    querylist.clear();
    querylist.addAll(sysUsers);
    model.addAttribute("querylist",querylist);
    return "sysUser/sysUserList";
}
public String list(@RequestParam(value = "realName",required = false)String realName,Model model){
    logger.info("查询条件【realName】="+realName+"查询用户信息");
    querylist.clear();
    if (realName!=null&&!realName.equals("")){
        for (SysUser sysUser:querylist){
            if (sysUser.getRealName().indexOf(realName)!=-1){
                querylist.add(sysUser);
            }
        }
    }else{
        querylist.addAll(sysUsers);
    }
    model.addAttribute("querylist",querylist);
    return "sysUser/sysUserList";
}
    /**
     * 捕获异常信息，跳转到error页面
     */
    public String handlerException(RuntimeException e,HttpServletRequest req){
        req.setAttribute("e",e);
        return "error";
    }
    /**
     * 一个抛出异常的接口
     */
    public String exLogin(@RequestParam String account,@RequestParam String password){
        SysUser user=sysUserService.login(account,password);
        if (null==user){
            throw new RuntimeException("用户名或密码不正确");
        }
        return "redirect:/user/toMain";
    }
    /**
     * 跳转到异常处理页面
     */
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }
}
